#include "morphing.h"
#include "ui_morphing.h"
#include "morphing_algo.h"

#include <TriMesh.h>
#include <QtGui/QtGui>
#include <debug.h>
using namespace CGF;

morphing::morphing(QWidget *parent) :
    Framework(parent),
    ui(new Ui::morphing)
{
	ui->setupUi(getUi().dockWidgetContents);
	targetMesh = NULL;
	connect(ui->pushButtonTargetMesh, SIGNAL(clicked()), this, SLOT(load_target_mesh()));
	connect(ui->pushButtonMorphing, SIGNAL(clicked()), this, SLOT(do_morphing_mesh()));
	this->setWindowTitle("Morphing");
	inter = 0;
	morp = NULL;
}

morphing::~morphing()
{
    delete ui;
	if(targetMesh)
		delete targetMesh;
	if(morp)
		delete morp;
}

void morphing::load_target_mesh()
{
	QString fileName = QFileDialog::getOpenFileName(this,
			tr("Open point cloud from file"), ".",
			tr("OBJ files (*.obj)"));

	 if(targetMesh) 
		 delete targetMesh;
	 targetMesh = TriMesh::read(fileName.toAscii());

	  morp = new morphing_algo(getMesh(), targetMesh);
	 morp->precompute_R_S();
}

void morphing::do_morphing_mesh()
{	
	inter +=0.1;
	if(inter >1)
		inter = 1;
	 morp->compute_interpolation_mesh(inter);

	 dbg(-1) <<inter;
	 this->updatGL();
}